<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>

<body>
<div id="watch-example">
    <p>
        Ask a yes/no question:
        <input v-model="question">
    </p>
    <p>{{ answer }}</p>
</div>
</body>
</html>
<script src="../js/vue.js"></script>
<script src="../node_modules/lodash/lodash.js"></script>
<script src="../node_modules/axios/dist/axios.js"></script>
<script>
    var vm = new Vue({
        el: '#watch-example',
        data: {
            question: '',
            answer: '没问不答'
        },
        watch: {
            // 如果 question 发生改变，这个函数就会运行
            question: function (newQuestion) {
                this.answer = 'Waiting for you to stop typing...';
                console.log(newQuestion)
                this.getAnswer()
            }
        },
        methods: {
            getAnswer:_.debounce(
                    function () {
                        if (this.question.indexOf('?') === -1) {
                            this.answer = '问题没有结束的问号'
                            return
                        }
                        this.answer = 'Thinking...';
                        var vm = this;
                        axios.get('https://yesno.wtf/api')
                                .then(function (response) {
                                    vm.answer = _.capitalize(response.data.answer)
                                })
                                .catch(function (error) {
                                    vm.answer = 'Error! Could not reach the API. ' + error
                                })
                    },
                    // 这是我们为用户停止输入等待的毫秒数
                    500
            )
//            fullName: function () {
//                return this.firstName + ' ' + this.lastName
//            }
//            fullName: {
//                get: function () {
//                    return this.firstName + ' ' + this.lastName
//                },
//                set: function (newValue) {
//                    var names = newValue.split(' ');
//                    this.firstName = names[0];
//                    this.lastName = names[names.length - 1]
//                }
//            }
        }
    });

</script>